<?php
/**
 * Jishupu-Laravel-Vue-Admin
 * @Author:技术铺<jishupu@qq.com>
 * @Date: 2021/11/11
 */

namespace App\Http\Requests\Example;

use App\Http\Requests\AttachmentRequest;
use App\Http\Requests\BaseRequest;
use App\Services\AttachmentService;
use App\Services\DictionaryService;
use Carbon\Carbon;
use Illuminate\Support\Facades\Auth;
use Illuminate\Validation\Rule;

/**
 * 普通表格演示模块表单验证
 * Class ExampleTableRequest
 * @package App\Http\Requests
 */
class ExampleTableRequest extends BaseRequest
{

    /**
     * 注入表单请求验证时自动校验
     *
     * @var bool
     */
    public $autoValidate = false;

    /**
     * 当前经过身份验证的用户是否可以执行请求操作
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * 规则
     *
     * @return array
     */
    public function rules()
    {
        return [
            'id' => 'required|integer',
            'created_user_id' => 'required|integer',
            'category_id' => 'required|integer',
            'switch' => 'nullable|boolean',
            'title' => "required|min:2|max:50",
            'cover_image' => [
                function ($attribute, $value, $fail) {
                    AttachmentRequest::validity(__('common.cover_image'), $value, $fail, 1);
                }
            ],
            'keywords' => 'nullable|max:500',
            'description' => 'nullable|max:200',
            'content' => 'nullable',
            'recommend_type' => 'nullable|max:50',
            'start_time' => 'nullable',
            'end_time' => 'nullable',
            'type' => [
                'nullable',
                'integer',
                Rule::in(DictionaryService::getItemsValuesArray('infoCategoryTypeVar')),
            ],
            'tree' => 'nullable',
            'file_ids' => [
                function ($attribute, $value, $fail) {
                    AttachmentRequest::validity(__('common.file'), $value, $fail, 5);
                }
            ],
            'province_id' => 'nullable|integer',
            'city_id' => 'nullable|integer',
            'county_id' => 'nullable|integer',
            'icon' => 'nullable',
            'href' => 'nullable|max:100',
            'counts' => 'nullable|integer',
            'image_list_ids' => 'nullable',
            'image_list' => 'nullable',
            'status' => 'required|boolean',
            'sort.*.id' => 'required|integer',
            'sort.*.sort' => 'required|integer',
        ];
    }

    /**
     * 定义场景
     * @var \string[][]
     */
    public $scenes = [
        'store' => [
            'category_id',
            'switch',
            'title',
            'cover_image',
            'keywords',
            'description',
            'content',
            'recommend_type',
            'start_time',
            'end_time',
            'type',
            'tree',
            'file_ids',
            'province_id',
            'city_id',
            'county_id',
            'icon',
            'href',
            'counts',
            'image_list',
            'image_list_ids',
            'status',
            'created_user_id',
            'created_at',
        ],
        'update' => [
            'category_id',
            'switch',
            'title',
            'cover_image',
            'keywords',
            'description',
            'content',
            'recommend_type',
            'start_time',
            'end_time',
            'type',
            'tree',
            'file_ids',
            'province_id',
            'city_id',
            'county_id',
            'icon',
            'href',
            'counts',
            'image_list',
            'image_list_ids',
            'status',
            'created_at',
        ],
        'destroy' => ['id'],
        'updateStatus' => ['status'],
    ];

    /**
     * 验证错误的自定义属性
     *
     * @return array
     */
    public function attributes()
    {
        return [
            'id' => __('common.id'),
            'category_id' => __('common.category'),
            'switch' => __('example/exampleTable.switch'),
            'title' => __('common.title'),
            'cover_image' => __('common.cover_image'),
            'keywords' => __('common.keywords'),
            'description' => __('common.description'),
            'content' => __('common.content'),
            'recommend_type' => __('common.recommend_type'),
            'start_time' => __('common.start_time'),
            'end_time' => __('common.end_time'),
            'type' => __('common.type'),
            'tree' => __('example/exampleTable.tree'),
            'file_ids' => __('common.file'),
            'province_id' => __('common.province_id'),
            'city_id' => __('common.city_id'),
            'county_id' => __('common.county_id'),
            'icon' => __('common.icon'),
            'href' => __('common.href'),
            'counts' => __('example/exampleTable.counts'),
            'image_list' => __('common.picture'),
            'image_list_ids' => __('example/exampleTable.image_list_ids'),
            'created_user_id' => __('common.created_user_id'),
            'created_at' => __('common.created_at'),
            'status' => __('common.status'),
            'sort.*.id' => __('common.id'),
        ];
    }

    /**
     * 验证前
     *
     * @return void
     */
    protected function prepareForValidation()
    {
        $this->offsetSet('created_user_id', Auth::id());
        if (isset($this->category_id) && empty($this->category_id)) $this->offsetSet('category_id', 0);
        if (isset($this->cover_image) && empty($this->cover_image)) $this->offsetSet('cover_image', 0);
        if (isset($this->keywords)) $this->offsetSet('keywords', join(',', $this->keywords));
        if (isset($this->recommend_type)) $this->offsetSet('recommend_type', join(',', $this->recommend_type));
        if (isset($this->created_at) && empty($this->created_at)) $this->offsetSet('created_at', Carbon::now());
        if (isset($this->counts) && empty($this->counts)) $this->offsetSet('counts', 0);
        if (isset($this->start_time) && empty($this->start_time)) $this->offsetSet('start_time', null);
        if (isset($this->end_time) && empty($this->end_time)) $this->offsetSet('end_time', null);
        if (isset($this->tree)) $this->offsetSet('tree', join(',', $this->tree));
        // 处理图集
        $getRequestPicture = AttachmentService::getRequestPicture($this->image_list);
        $this->offsetSet('image_list_ids', $getRequestPicture['image_list_ids']);
        $this->offsetSet('image_list', $getRequestPicture['image_list']);
    }
}


